package com.ap.core.ldap.domain;


import org.springframework.ldap.core.AttributesMapper;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;

public class LdapZappUserAttributeMapper implements AttributesMapper {

    @Override
    public Object mapFromAttributes(Attributes attributes) throws NamingException {
        final LdapZappUser ldapZappUser = new LdapZappUser();

        ldapZappUser.setCommonName(getValue(attributes.get(ldapZappUser.getCommonName().getLdapAttributeName())));
        ldapZappUser.setEmailId(getValue(attributes.get(ldapZappUser.getEmailId().getLdapAttributeName())));
        ldapZappUser.setFirstName(getValue(attributes.get(ldapZappUser.getFirstName().getLdapAttributeName())));
        ldapZappUser.setLastName(getValue(attributes.get(ldapZappUser.getLastName().getLdapAttributeName())));
        ldapZappUser.setMobileNumber(getValue(attributes.get(ldapZappUser.getMobileNumber().getLdapAttributeName())));
        ldapZappUser.setUserName(getValue(attributes.get(ldapZappUser.getUserName().getLdapAttributeName())));
        ldapZappUser.setAgreedTermsAndConditions((getValue(attributes.get(ldapZappUser.getAgreedTermsAndConditions().getLdapAttributeName()))));
        ldapZappUser.setPassword(attributes.get(ldapZappUser.getPassword().getLdapAttributeName()) == null ? null : attributes.get(ldapZappUser.getPassword().getLdapAttributeName()).get());
        ldapZappUser.setUserHash((getValue(attributes.get(ldapZappUser.getUserHash().getLdapAttributeName()))));

        return  ldapZappUser;
    }

    private String getValue(Attribute attribute) throws NamingException{
        return attribute == null ? null : (String)attribute.get();
    }
}
